$(function() {
	admin.communityBase.init();
});
Namespace('admin.communityBase', {
	init : function() {
		easyui.util.initDatagrid('#datagrid', {
			onDblClickRow : function() {
			}
		});
	},
	query : function() {
		if (!$('#query-form').form('validate'))
			return;
		var data = $('#query-form').form('jsonObject');
		$('#datagrid').datagrid('load', data);
	},
	detail:function(id) {
		if (id != null) {
			top.addTab(id,'详情', contextPath+'/community/base/detail?id=' + id);
		}
	},
	subsetAt:function(communityId,parentId) {
		if (communityId != null&&parentId != null) {
			top.addTab(communityId,'分类列表', contextPath+'/community/addr/subset/'+communityId+'/'+parentId);
		}
	},
	setIsShow: function (id,isShow) {
		var tip = "确定屏蔽吗";
		if (isShow) {
			tip = "确定解除屏蔽吗";
		}
		$.messager.confirm('信息提示', tip, function (success) {
			if (!success) {
				return;
			}
            var url = contextPath + '/community/base/show';
			if (!isShow) {
				url = contextPath + '/community/base/hide';
			}
			$('#datagrid').datagrid('loading');
			$.post(url, {'id': id}, function (result) {
				$('#datagrid').datagrid('loaded');
				if (result.code == 1) {
					top.showInfo('操作成功');
					var pageopt = $('#datagrid').datagrid('getPager').data("pagination").options;
					console.log(pageopt.pageNumber);
                    admin.communityBase.query();
				} else {
					top.showInfo(result.msg);
				}
			});

		});
	},
	setIsOpen: function (id) {
		var tip = "确定通过吗";
		$.messager.confirm('信息提示', tip, function (success) {
			if (!success) {
				return;
			}
            var url = contextPath + '/community/base/isOpen';
			$('#datagrid').datagrid('loading');
			$.post(url, {'id': id}, function (result) {
				$('#datagrid').datagrid('loaded');
				if (result.code == 1) {
					top.showInfo('操作成功');
					var pageopt = $('#datagrid').datagrid('getPager').data("pagination").options;
					console.log(pageopt.pageNumber);
                    admin.communityBase.query();
				} else {
					top.showInfo(result.msg);
				}
			});

		});
	},
	createSuccess: function (id) {
		var tip = "确定通过创建吗";
		$.messager.confirm('信息提示', tip, function (success) {
			if (!success) {
				return;
			}
            var url = contextPath + '/community/base/createSuccess';
			$('#datagrid').datagrid('loading');
			$.post(url, {'id': id}, function (result) {
				$('#datagrid').datagrid('loaded');
				if (result.code == 1) {
					top.showInfo('操作成功');
					var pageopt = $('#datagrid').datagrid('getPager').data("pagination").options;
					console.log(pageopt.pageNumber);
                    admin.communityBase.query();
				} else {
					top.showInfo(result.msg);
				}
			});

		});
	},
	addImg:function(id){
		getDialog('query-dialog').dialog({
			href:contextPath+'/community/base/imgUpload',
			title: '选择文件',
		    width: 350,
		    height: 250,
		    buttons:[{
				text:'上传',
				handler:function(){
					uploaderStart(id);
				}
			}]
		});
		
	},
	uploadImg:function(id) {
		console.info("start");
		$('#filedata-form').form('submit',{
			onSubmit: function(){
				console.info($('#uploadkey1').val());
				if ($('#uploadkey1').val()=='') {
					top.showInfo('请选择图片!');
					return false;
				}
				$('#uploadBtn').val('上传中...').attr('disabled',true);
			}, 
			success:function(result){
				$('#uploadBtn').val('上传').attr('disabled',false);
				var isjson = typeof(result) == "object" && Object.prototype.toString.call(result).toLowerCase() == "[object object]" && !result.length;
				console.info("---------:"+isjson);
				console.info(result);
				if (!isjson) {
					result = JSON.parse(result);
				}
				var uploadResults=result.map.uploadResults;
				var imgHtml=[];
				var img=[];
				for(var i=0;i<uploadResults.length;i++){
					img.push(uploadResults[i].filePath);
					imgHtml.push('<img style="width:260px;height:260px;" src="http://qquan-hn-crazy.oss-cn-shenzhen.aliyuncs.com'+uploadResults[i].filePath+'">');
					imgHtml.push("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;")
				}
				$('#showpic').html(imgHtml.join(""));
				var url = contextPath + '/community/base/updateImg';
				$.post(url, {'id': id,'imgPath':img.join(";")}, function (result) {
					$('#datagrid').datagrid('loaded');
					if (result.code == 1) {
						top.showInfo('操作成功');
					} else {
						top.showInfo(result.msg);
					}
				});
			}
		});
		
	},
	/**
	 * 开启抽奖
	 */
	upLottery: function (id) {
        var url = contextPath + '/community/base/upLottery';
		$('#datagrid').datagrid('loading');
		$.post(url, {'id': id}, function (result) {
			$('#datagrid').datagrid('loaded');
			if (result.code == 1) {
				top.showInfo('开启成功');
				var pageopt = $('#datagrid').datagrid('getPager').data("pagination").options;
                admin.communityBase.query();
			} else {
				top.showInfo(result.msg);
			}
		});
	},
	/**
	 * 取消抽奖
	 */
	downLottery: function (id) {
        var url = contextPath + '/community/base/downLottery';
		$('#datagrid').datagrid('loading');
		$.post(url, {'id': id}, function (result) {
			$('#datagrid').datagrid('loaded');
			if (result.code == 1) {
				top.showInfo('取消成功');
				var pageopt = $('#datagrid').datagrid('getPager').data("pagination").options;
                admin.communityBase.query();
			} else {
				top.showInfo(result.msg);
			}
		});
	}
});